import os
import glob
import numpy
import operator

def _print_output(path):
    tmp_file = glob.glob(path + '/*.npz')
    for i in range(len(tmp_file)):
        statinfo = os.stat(tmp_file[i])
	tmp_file[i] = tmp_file[i],statinfo.st_size,statinfo.st_mtime
    
    tmp_file.sort(key=operator.itemgetter(2))    
    tmp_file = tmp_file[len(tmp_file)-2]
    data = numpy.load(tmp_file[0])
    f = data['flight']
    a = data['airport']
    u = data['u']
    v = data['v']
    a = data['a']
    g = data['g']
    tmp = ''
    for i in range(len(f)):
        if int(g[i]) != 0:
            tmp += "Flight id " + str(f[i].id) + ", " + f[i].code + " delayed by " + str(g[i]) + " minutes on ground"
        if int(a[i]) != 0:
            tmp += " and " + str(a[i]) + " in air"
        tmp += "\n"
    print tmp


def make_schedule(path):
    tex_file = open('schedule/main.tex','w')
    tmp_file = glob.glob(path + '/*.npz')
    for i in range(len(tmp_file)):
        statinfo = os.stat(tmp_file[i])
	tmp_file[i] = tmp_file[i],statinfo.st_size,statinfo.st_mtime
    
    tmp_file.sort(key=operator.itemgetter(2))    
    tmp_file = tmp_file[len(tmp_file)-3]
    print tmp_file
    data = numpy.load(tmp_file[0])
    f = data['flight']
    a = data['airport']
    u = data['u']
    v = data['v']
    a = data['a']
    g = data['g']
    
    tmp = ''
    tmp += "\documentclass[a4paper 40pt]{article} \n \usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry} \n \usepackage{amsmath} \n \usepackage[usenames,dvipsnames]{color} \n \usepackage[pdftex]{graphicx} \n \usepackage{lscape} \n \usepackage{verbatim}"

    tmp += "\\begin{document}"

    tmp += "\\begin{tabular}{|l|l|l|l|l|l|} \hline \n Flight & From & Time & To & Time & Next Flight\\\ \hline "

    for i in range(len(f)):
        tmp += "\n " + f[i].code + " & " + f[i].k_d.code + " & " + str(f[i].d + g[i]) + " & " + f[i].k_a.code + " & " + str(f[i].r + g[i] + a[i]) 
        if f[i].next_flight != None:
            tmp += " & " + f[i].next_flight.code
        else:
            tmp += " & " + "None"
        tmp += "\\\ \hline"


    tmp += "\end{tabular}"
    tmp += "\end{document}"
    tex_file.write(tmp)
    


def make_initial_schedule(flights):
    f = flights
    tex_file = open('schedule/main.tex','w')
    tmp = ''
    tmp += "\documentclass[a4paper 40pt]{article} \n \usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry} \n \usepackage{amsmath} \n \usepackage[usenames,dvipsnames]{color} \n \usepackage[pdftex]{graphicx} \n \usepackage{lscape} \n \usepackage{verbatim}"

    tmp += "\\begin{document}"

    tmp += "\\begin{tabular}{|l|l|l|l|l|l|} \hline \n Flight & From & Time & To & Time & Next Flight\\\ \hline "

    for i in range(len(f)):
        tmp += "\n " + f[i].code + " & " + f[i].k_d.code + " & " + str(f[i].d) + " & " + f[i].k_a.code + " & " + str(f[i].r) 
        if f[i].next_flight != None:
            tmp += " & " + f[i].next_flight.code
        else:
            tmp += " & " + "None"
        tmp += "\\\ \hline"


    tmp += "\end{tabular}"
    tmp += "\end{document}"
    tex_file.write(tmp)
if __name__ == "__main__":
    make_schedule('../data/data_test2')
